package com.fivefu.core.data.controller;

import com.alibaba.fastjson.JSON;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.web.controller.BaseController;
import com.fivefu.base.web.vo.ResultInfo;
import com.fivefu.core.data.services.DbFfskyeyeChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


/**
 * <p>
 * 通道 前端控制器
 * </p>
 *
 * @author 
 * @since 2022-09-02 13:38:22
 */
@Api(tags = "通道汇总")
@RestController
@RequestMapping("/ffskyeyeChannel")
public class DbFfskyeyeChannelController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(DbFfskyeyeChannelController.class);

    @Autowired
    private DbFfskyeyeChannelService dbFfskyeyeChannelService;

    @ApiOperation(value = "通过通道唯一标识查询播放地址",httpMethod = "POST",response = ResultInfo.class,notes = "通过通道唯一标识查询播放地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "channelCode",value = "通道唯一标识",paramType = "query",required = true, dataType = "String"),
            @ApiImplicitParam(name = "protocal",value = "取流协议(共同协议：rtsp,rtmp,hls,海康额外有：hik,ws,大华额外有：FLV_HTTP)",paramType = "query",required = false,dataType = "String"),
            @ApiImplicitParam(name = "subType",value = "码流类型(0:主码流,1:辅流1,2:辅流2。默认为0主码流)只有大华有",paramType = "query",required = false,dataType = "int"),
    })
    @RequestMapping(path = "/queryUrlByChannelCode",method = RequestMethod.POST)
    public ResultInfo queryUrlByChannelCode(String channelCode,String protocal,Integer subType) {
        logger.info("通过通道唯一标识查询播放地址请求参数为：" + JSON.toJSONString(request.getParameterMap()));
        try {
            if (StrUtils.isNull(channelCode)) {
                return ResultInfo.renderError("通道唯一标识不能为空");
            }
            String url = dbFfskyeyeChannelService.queryUrlByChannelCode(channelCode,protocal,subType);
            return ResultInfo.renderSuccess(url);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("通过通道唯一标识查询播放地址失败");
    }

    @ApiOperation(value = "查询多个通道流地址",httpMethod = "POST",response = ResultInfo.class,notes = "查询多个通道流地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "channelCodes",value = "通道唯一标识集（多个用逗号隔开）",paramType = "query",required = true, dataType = "String"),
            @ApiImplicitParam(name = "protocal",value = "取流协议(共同协议：rtsp,rtmp,hls,海康额外有：hik,ws,大华额外有：FLV_HTTP)",paramType = "query",required = false,dataType = "String"),
            @ApiImplicitParam(name = "subType",value = "码流类型(0:主码流,1:辅流1,2:辅流2。默认为0主码流)只有大华有",paramType = "query",required = false,dataType = "int")
    })
    @RequestMapping(path = "/getUrlBychannelCodes",method = RequestMethod.POST)
    public ResultInfo getUrlBychannelCodes(String channelCodes,String protocal,Integer subType) {
        logger.info("查询多个通道流地址请求参数为：" + JSON.toJSONString(request.getParameterMap()));
        try {
            if (StrUtils.isNull(channelCodes)) {
                return ResultInfo.renderError("通道唯一标识不能为空");
            }
            Map<String,String> resultList = dbFfskyeyeChannelService.getUrlBychannelCodes(channelCodes,protocal,subType);
            return ResultInfo.renderSuccess(resultList);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查询多个通道流地址失败");
    }

    @ApiOperation(value = "通道信息汇总入库",httpMethod = "GET",response = ResultInfo.class,notes = "通道信息汇总入库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "设备平台：海康，大华",paramType = "query",required = true, dataType = "String"),
            @ApiImplicitParam(name = "userId",value = "用户编号",paramType = "query",required = true, dataType = "String")
    })
    @RequestMapping(path = "/channelCollect",method = RequestMethod.GET)
    public ResultInfo channelCollect(String type,String userId) {
        try {
            if (StrUtils.isNull(type)) {
                return ResultInfo.renderError("设备平台不能为空");
            }
            if (StrUtils.isNull(userId)) {
                return ResultInfo.renderError("用户编号不能位空");
            }
            dbFfskyeyeChannelService.channelCollect(type,userId);
            return ResultInfo.renderSuccess("通道信息汇总入库成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return  ResultInfo.renderError("通道信息汇总入库失败");
    }

    @ApiOperation(value = "获取视频回放URI",httpMethod = "GET",response = ResultInfo.class,notes = "获取视频回放URI")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "channelCode",value = "通道唯一标识",paramType = "query",required = true, dataType = "String"),
            @ApiImplicitParam(name = "stime",value = "回放开始时间(yyyy-MM-dd HH:mm:ss)",paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "etime",value = "回放结束时间(yyyy-MM-dd HH:mm:ss)",paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "scheme",value = "协议类型，共有：rtsp、rtmp、hls三种,海康额外：hik,ws(海康默认hik,大华默认rtsp)",paramType = "query", required = false, dataType = "String")
    })
    @RequestMapping(path = "/queryPlaybackURI",method = RequestMethod.GET)
    public ResultInfo queryPlaybackURI(String channelCode,String stime,String etime,String scheme) {
        try {
            logger.info("获取视频回放URI请求参数为：" + JSON.toJSONString(request.getParameterMap()));
            if (StrUtils.isNull(channelCode)) {
                return ResultInfo.renderError("通道唯一标识不能为空");
            }
            if (StrUtils.isNull(stime)) {
                return ResultInfo.renderError("回放开始时间不能为空");
            }
            if (StrUtils.isNull(etime)) {
                return ResultInfo.renderError("回放结束时间不能为空");
            }
            String url = dbFfskyeyeChannelService.queryPlaybackURI(channelCode,stime,etime,scheme);
            return ResultInfo.renderSuccess(url);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return  ResultInfo.renderError("获取视频回放URI失败");
    }

    @ApiOperation(value = "同步通道在线状态",httpMethod = "GET",response = ResultInfo.class,notes = "同步通道在线状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "设备平台：海康，大华",paramType = "query",required = true, dataType = "String"),
            @ApiImplicitParam(name = "userId",value = "用户编号",paramType = "query",required = true, dataType = "String")
    })
    @RequestMapping(path = "/synChannelOnlineStatus",method = RequestMethod.GET)
    public ResultInfo synChannelOnlineStatus(String type,String userId) {
        try {
            if (StrUtils.isNull(type)) {
                return ResultInfo.renderError("设备平台不能为空");
            }
            if (StrUtils.isNull(userId)) {
                return ResultInfo.renderError("用户编号不能位空");
            }
            dbFfskyeyeChannelService.synChannelOnlineStatus(type,userId);
            return ResultInfo.renderSuccess("通道信息汇总入库成功");
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return  ResultInfo.renderError("通道信息汇总入库失败");
    }

}
